import 'package:flutter/material.dart';
import 'package:flutter_shop/eventbus/event_bus.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/pages/category/category_tab_page/entity/category_data.dart';
import 'package:flutter_shop/pages/category/category_tab_page/provider/left_menu_provider.dart';
import 'package:flutter_shop/pages/category/category_tab_page/provider/sub_category_goods_page_provider.dart';
import 'package:flutter_shop/pages/category/category_tab_page/provider/sub_category_provider.dart';
import 'package:flutter_shop/pages/category/category_tab_page/widget/sub_category_goods.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:provider/provider.dart';

///左侧菜单
class LeftMenu extends StatefulWidget {
  ///改变左菜单索引
  static const String CHANGE_INDEX = "LeftMenuChangeIndex";

  List<CategoryData> mDatas;

  LeftMenu(this.mDatas, {Key key}) : super(key: key);

  @override
  _LeftMenuState createState() => _LeftMenuState();
}

class _LeftMenuState extends BaseState<LeftMenu, BasePresenter> {
  int _currentIndex = 0;

  @override
  bool get useEventBus => true;

  @override
  void onEventBus(EventMessage event) {
    switch (event.tag) {
      case LeftMenu.CHANGE_INDEX:
        if (widget.mDatas != null && widget.mDatas.isNotEmpty) {
          CategoryData item = widget.mDatas[event.int1];
          _checkItem(event.int1, context, item);
        }
        break;

      default:
        break;
    }
  }

  @override
  void buildComplete() {
    Utils.postDelayed(0, () {
      if (widget.mDatas != null && widget.mDatas.isNotEmpty) {
        CategoryData item = widget.mDatas[_currentIndex];
        _checkItem(_currentIndex, context, item);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    _currentIndex = Provider.of<LeftMenuProvider>(context).currentIndex;

    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Borders.right,
      ),
      child: ListView.builder(
        itemCount: widget.mDatas.length,
        itemBuilder: _buildItem,
      ),
    );
  }

  //菜单item
  Widget _buildItem(context, index) {
    CategoryData item = widget.mDatas[index];

    return InkWell(
      onTap: () {
        _checkItem(index, context, item);
      },
      child: Container(
        padding: Paddings.common,
        decoration: BoxDecoration(
          color: _currentIndex == index ? CommonColors.common_bg : Colors.white,
          border: Borders.bottom,
        ),
        child: Text(item.mallCategoryName),
      ),
    );
  }

  ///切换item
  void _checkItem(index, context, CategoryData item) {
    Provider.of<LeftMenuProvider>(context, listen: false).changeIndex(index);

    //设置子分类数据
    Provider.of<SubCategoryProvider>(context, listen: false).setSubCategoryList(item.bxMallSubDto);
    //设置大分类和子分类id
    Provider.of<SubCategoryGoodsPageProvider>(context, listen: false).setCategoryIdAndCategorySubId(item.mallCategoryId, "");
    //刷新商品列表
    EventBus().post(EventMessage(SubCategoryGoods.REFRESH, str1: item.mallCategoryId, str2: ""));
  }
}
